package com.wty.cmims.controller;

import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.pojo.User;
import com.wty.cmims.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Tag(name = "账号管理", description = "社区医疗信息管理系统api接口")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户账号注册数据信息")
    @Parameter(name = "name",description = "姓名")
    @Parameter(name = "account",description = "账号")
    @Parameter(name = "password",description = "密码")
    @Parameter(name = "role",description = "角色")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Map<String, Object>> userRegister(
            @RequestParam(name = "name") String name,
            @RequestParam(name = "account") String account,
            @RequestParam(name = "phone") String phone,
            @RequestParam(name = "password") String password,
            @RequestParam(name = "role") Integer role
            ){
        return userService.userRegister(name,account,phone,password,role);
    }

    // 用户登录
    @PostMapping  ("/login")
    @Operation(summary = "用户登录", description = "用户账号登录数据信息")
    @Parameter(name = "account",description = "账号")
    @Parameter(name = "password",description = "密码")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Map<String, Object>> userLogin(
            @RequestParam(name = "account") String account,
            @RequestParam(name = "password") String password
    ){
        return userService.userLogin(account,password);
    }

    // 用户信息更新
    @PutMapping ("/update")
    @Operation(summary = "用户信息修改", description = "用户信息修改")
    @Parameter(name = "user",description = "用户信息")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<User> userUpdate(@RequestBody User user){
        return userService.userUpdate(user);
    }

    // 修改密码
    @PutMapping("/updatePassword")
    @Operation(summary = "修改密码", description = "修改密码")
    @Parameter(name = "account",description = "账号")
    @Parameter(name = "oldPassword",description = "旧密码")
    @Parameter(name = "newPassword",description = "新密码")
    @ApiResponse(description = "处理结果",content = {
            @Content(
                    mediaType="application/json",
                    schema= @Schema(implementation = ResponseResult.class)
            )
    })
    public ResponseResult<Void> updatePassword(
            @RequestParam(name = "account") String account,
            @RequestParam(name = "oldPassword") String oldPassword,
            @RequestParam(name = "newPassword") String newPassword
            ){
        return userService.updatePassword(account,oldPassword,newPassword);
    }
}